import java.util.Scanner;

public class CA {
    public static void main(String[] args) {
        Coustomer c1 = new Coustomer("c1",9999);
        Coustomer c2 = new Coustomer("c2",4444);
        Thread t1= new Thread(c1,"t1");
        Thread t2 = new Thread(c1,"t2");
        t1.start();
        t2.start();
        c2.withdraw();
        c2.setname("c3");
        c2.withdraw();
        c2.update();
        c2.withdraw();
    }
    static class Coustomer implements Runnable{
        public void run(){
            System.out.println("欢迎"+Thread.currentThread().getName());
            withdraw(100,56,Thread.currentThread());
        }
        private String name;
        private Account account;
        public Coustomer(String name,double monney){
            this.name=name;
            account=new Account(name,monney);
        }
        public String getname(){
            return name;
        }
        public void setname(String name){
            this.name=name;
        }
        public void update(){
            account.setName(name);
        }
        public synchronized void withdraw(){
            Scanner sc = new Scanner(System.in);
            System.out.println("取出多少钱?");
            double n = sc.nextDouble();
            System.out.println("取出多少次?");
            int j = sc.nextInt();
            for (int i=0;i<j;i++){
                account.withdraw(n,this);
            }
        }
        public synchronized void withdraw(double n ,int j,Thread thread){
            System.out.println(thread.getName()+"开始取钱");
            for (int i=0;i<j;i++){
                account.withdraw(n,this);
            }
        }
    }
    static class Account{
        private String name;
        private double money;
        public Account(String name,double money){
            this.name=name;
            this.money=money;
        }
        public void getter(){
            System.out.println(name+"余额:"+money);
        }
        public void setter(double money){
            this.money=money;
        }
        public void setName(String name){
            this.name=name;
        }
        public boolean equals(Object o){
            return name.equals(((Coustomer)o).getname());
        }
        public void withdraw(double n,Object o){
            if (this.equals(o)){
                if(money>=n){
                    money-=n;
                    System.out.println("取出了:"+n+",剩:"+money);
                }
                else {
                    System.out.println("余额不足");
                }
            }
            else{
                System.out.println("无权取款");
            }
        }
    }
}